package com.reservation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reservation.common.BusinessException;
import com.reservation.entity.Guide;
import com.reservation.entity.GuideReservation;
import com.reservation.mapper.GuideMapper;
import com.reservation.mapper.GuideReservationMapper;
import com.reservation.service.GuideService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 解说员服务实现类
 */
@Service
public class GuideServiceImpl implements GuideService {

    @Autowired
    private GuideMapper guideMapper;
    
    @Autowired
    private GuideReservationMapper guideReservationMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Guide addGuide(Guide guide) {
        // 设置创建时间和更新时间
        guide.setCreateTime(LocalDateTime.now());
        guide.setUpdateTime(LocalDateTime.now());
        
        // 设置默认状态
        if (StringUtils.isBlank(guide.getStatus())) {
            guide.setStatus("AVAILABLE");
        }
        
        // 保存解说员信息
        guideMapper.insert(guide);
        
        return guide;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Guide updateGuide(Guide guide) {
        // 检查解说员是否存在
        Guide existGuide = guideMapper.selectById(guide.getId());
        if (existGuide == null) {
            throw new BusinessException("解说员不存在");
        }
        
        // 设置更新时间
        guide.setUpdateTime(LocalDateTime.now());
        
        // 更新解说员信息
        guideMapper.updateById(guide);
        
        // 返回更新后的解说员信息
        return guideMapper.selectById(guide.getId());
    }

    @Override
    public Guide getGuideById(Long id) {
        Guide guide = guideMapper.selectById(id);
        if (guide == null) {
            throw new BusinessException("解说员不存在");
        }
        return guide;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteGuide(Long id) {
        // 检查解说员是否存在
        Guide guide = guideMapper.selectById(id);
        if (guide == null) {
            throw new BusinessException("解说员不存在");
        }
        
        // 删除解说员
        return guideMapper.deleteById(id) > 0;
    }

    @Override
    public IPage<Guide> getGuideList(Page<Guide> page, String name, String language, String status) {
        LambdaQueryWrapper<Guide> queryWrapper = new LambdaQueryWrapper<>();
        
        // 添加查询条件
        if (StringUtils.isNotBlank(name)) {
            queryWrapper.like(Guide::getName, name);
        }
        if (StringUtils.isNotBlank(language)) {
            queryWrapper.eq(Guide::getLanguage, language);
        }
        if (StringUtils.isNotBlank(status)) {
            queryWrapper.eq(Guide::getStatus, status);
        }
        
        // 按创建时间降序排序
        queryWrapper.orderByDesc(Guide::getCreateTime);
        
        // 执行查询
        return guideMapper.selectPage(page, queryWrapper);
    }

    @Override
    public List<Guide> getAllGuides() {
        LambdaQueryWrapper<Guide> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Guide::getCreateTime);
        return guideMapper.selectList(queryWrapper);
    }

    @Override
    public List<Guide> getAvailableGuides() {
        LambdaQueryWrapper<Guide> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Guide::getStatus, "AVAILABLE");
        queryWrapper.orderByDesc(Guide::getCreateTime);
        return guideMapper.selectList(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Guide updateGuideStatus(Long id, String status) {
        // 检查解说员是否存在
        Guide guide = guideMapper.selectById(id);
        if (guide == null) {
            throw new BusinessException("解说员不存在");
        }
        
        // 更新状态
        Guide updateGuide = new Guide();
        updateGuide.setId(id);
        updateGuide.setStatus(status);
        updateGuide.setUpdateTime(LocalDateTime.now());
        guideMapper.updateById(updateGuide);
        
        // 返回更新后的解说员信息
        return guideMapper.selectById(id);
    }

    @Override
    public boolean checkGuideAvailable(Long guideId, LocalDate date, String timeSlot) {
        // 检查解说员是否存在
        Guide guide = guideMapper.selectById(guideId);
        if (guide == null) {
            throw new BusinessException("解说员不存在");
        }
        
        // 检查解说员状态是否可用
        if (!"AVAILABLE".equals(guide.getStatus())) {
            return false;
        }
        
        // 检查该时间段是否已被预约
        LambdaQueryWrapper<GuideReservation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GuideReservation::getGuideId, guideId);
        queryWrapper.eq(GuideReservation::getReservationDate, date);
        queryWrapper.eq(GuideReservation::getTimeSlot, timeSlot);
        queryWrapper.in(GuideReservation::getStatus, "PENDING", "APPROVED");
        
        return guideReservationMapper.selectCount(queryWrapper) == 0;
    }

    @Override
    public boolean guideCancel(Long id) {
        // 检查预约是否存在
        GuideReservation reservation = guideReservationMapper.selectById(id);
        if (reservation == null) {
            throw new BusinessException("预约不存在");
        }

        // 检查预约状态是否可取消
        if ("CANCELED".equals(reservation.getStatus()) || "REJECTED".equals(reservation.getStatus())) {
            throw new BusinessException("该预约已取消或被拒绝");
        }

        // 更新状态为取消
        GuideReservation updateReservation = new GuideReservation();
        updateReservation.setId(id);
        updateReservation.setStatus("CANCELED");
        updateReservation.setUpdateTime(LocalDateTime.now());

        return guideReservationMapper.updateById(updateReservation) > 0;
    }

}